package com.ybu.controller;

import com.ybu.common.Constants;
import com.ybu.common.JwtUtils;
import com.ybu.common.R;
import com.ybu.pojo.dto.UserInfoDTO;
import com.ybu.pojo.dto.UserLoginDTO;
import com.ybu.pojo.dto.RegisterDTO;
import com.ybu.pojo.dto.UpdatePwdDTO;
import com.ybu.pojo.vo.UserLoginVO;
import com.ybu.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController //整个控制器返回的都是数据
@RequestMapping("/api/user") // 访问的url的上级或者上两级目录
@Slf4j //日志的优先级默认是info
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("reg")
    public R<Void> register(@RequestBody @Validated RegisterDTO registerDTO){
        log.debug("获取到的注册信息：{}",registerDTO);
        userService.register(registerDTO);
        return R.ok();
    }
    @PostMapping("login")
    public R<UserLoginVO> login(@RequestBody  @Validated UserLoginDTO userLoginDTO){
//        userService.login(loginDTO);
        UserLoginVO userLoginVO = userService.login(userLoginDTO);

        //登录成功了
        return R.ok(userLoginVO);
//        return R.ok(loginDTO);
    }
    @PostMapping("updatePwd")
    public R<Void> updatePwd(@RequestBody @Validated UpdatePwdDTO updatePwdDTO,
                             HttpServletRequest request){
//修改密码是要明确用户身份：知道是谁在修改密码
        String token = request.getHeader(Constants.TOKEN_HEADER);
//获取用户身份:登录的用户身份
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
//调用业务层处理密码修改的业务
        userService.updatePwd(updatePwdDTO, userLoginVO);

        return R.ok();
    }
    @PostMapping  ("info")
    public R<Void> editInfo(@RequestBody @Validated UserInfoDTO userInfoDTO, HttpServletRequest request){
        String token =request.getHeader(Constants.TOKEN_HEADER);
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        userService.editInfo(userInfoDTO,userLoginVO);
        return R.ok();
    }
    @PutMapping("updateAvatar")
    public R<Void> updateAvatar(@RequestBody MultipartFile avatar, HttpServletRequest request){
//修改密码是要明确用户身份：知道是谁在修改密码
        String token = request.getHeader(Constants.TOKEN_HEADER);

//获取用户身份:登录的用户身份
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
//调用业务层处理密码修改的业务
        userService.updateAvatar(avatar, userLoginVO);
        return R.ok();
    }
//    @PostMapping("put")
//    public R<Void> updatePwd(@RequestBody  @Validated UpdatePwdDTO updatePwdDTO, HttpServletRequest request){
//        request.getHeader(Constant.)
//
//    }


//    @PostMapping("change_avatar")
//            public R<String> changeAvatar(HttpSession session,@RequestParam("file") MultipartFile file)
//    {
//        if(file.isEmpty()){
//            throw new FileEmptyException("文件为空");
//        }
//   String parent=session.getServletContext().getRealPath("upload");
//        File dir = new File(parent);
//        if(!dir.exists()){
//            dir.mkdir();
//        }
//        String originalFilename = file.getOriginalFilename();
//        return R.ok();
//    }


}

